package com.liaogui.web.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟后台接口
 * @author liaogui
 * @date 2025/2/27 22:54
 */
@RestController
@RequestMapping("/admin/")
@RequiredArgsConstructor
public class SimulateAdminController {

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 活动信息入缓存
     *
     * @return {@link String}
     */
    @RequestMapping("/activity/cache")
    public String cacheActivityInfo() {
        // 使用hash存储可以读写特定的字段
        Map<String, Object> map = new HashMap<>();
        map.put("actId", "ACT202502281645");
        map.put("actName", "大转盘抽奖活动");
        map.put("actStartTime", "2025-02-28 16:45:00");
        map.put("actEndTime", "2025-03-05 16:45:00");
        map.put("status", "activity");
        redisTemplate.opsForHash().putAll("DRAW:ACT202502281645", map);
        return "活动信息入缓存成功";
    }

    /**
     * 写奖品信息
     *
     * @return {@link String}
     */
    @RequestMapping("/prize/cache")
    public String cachePrizeInfo() {
        redisTemplate.execute(new SessionCallback<>() {

            @Override
            public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
                redisTemplate.multi();
                try {
                    String key = "DRAW:PRIZE:ACT202502281645";
                    // 先使用Sorted Set存储所有的奖品ID
                    List<String> ids = Arrays.asList("PRIZE1001", "PRIZE1002", "PRIZE1003", "PRIZE1004", "PRIZE1005");
                    redisTemplate.opsForList().rightPushAll(key, ids.toArray(new String[ids.size()]));

                    // 使用hash存储可以读写特定的字段
                    int prizeWeight = 10;
                    for (String id : ids) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("prizeId", id);
                        map.put("prizeName", "奖品" + id);
                        map.put("prizeDesc", "这是一个奖品");
                        map.put("prizeValue", 100);
                        map.put("prizeStatus", "prize");
                        // 权重
                        map.put("prizeWeight", prizeWeight);
                        redisTemplate.opsForHash().putAll(key + ":" + id, map);
                        prizeWeight += 10;
                    }
                    redisTemplate.exec();
                }catch (Exception e) {
                    // 回滚
                    redisTemplate.discard();
                    throw new RuntimeException("奖品信息入缓存失败", e);
                }
                return null;
            }
        });
        return "奖品信息入缓存成功";
    }
}
